﻿#include <windows.h>

// 窗口过程函数
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
    switch (uMsg) {
        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
            
        case WM_PAINT: {
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hwnd, &ps);
            
            // 绘制窗口文本
            RECT rect;
            GetClientRect(hwnd, &rect);
            DrawTextW(hdc, L"极简Win32宽字符窗体", -1, &rect, 
                     DT_CENTER | DT_VCENTER | DT_SINGLELINE);
            
            EndPaint(hwnd, &ps);
            return 0;
        }
    }
    return DefWindowProcW(hwnd, uMsg, wParam, lParam);
}

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow) {
    // 注册窗口类
    const wchar_t CLASS_NAME[] = L"SimpleWindowClass";
    
    WNDCLASSW wc = {};
    wc.lpfnWndProc = WindowProc;
    wc.hInstance = hInstance;
    wc.lpszClassName = CLASS_NAME;
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    wc.hCursor = LoadCursorW(NULL, IDC_ARROW);
    
    // 加载 shell32.dll 中的 41 号图标
    wc.hIcon = (HICON)LoadImageW(
        NULL, 
        L"shell32.dll", 
        IMAGE_ICON, 
        0, 0, 
        LR_LOADFROMFILE | LR_DEFAULTSIZE
    );
    
    // 如果直接加载失败，尝试通过 ExtractIconW 获取
    if (!wc.hIcon) {
        HMODULE hShell32 = LoadLibraryW(L"shell32.dll");
        if (hShell32) {
            wc.hIcon = (HICON)LoadImageW(
                hShell32,
                MAKEINTRESOURCE(41),
                IMAGE_ICON,
                0, 0,
                LR_DEFAULTSIZE
            );
            FreeLibrary(hShell32);
        }
    }
    
    // 如果还是失败，使用默认图标
    if (!wc.hIcon) {
        wc.hIcon = LoadIconW(NULL, IDI_APPLICATION);
    }
    
    RegisterClassW(&wc);
    
    // 创建窗口
    HWND hwnd = CreateWindowExW(
        0,
        CLASS_NAME,
        L"极简Win32宽字符窗体",
        WS_BORDER,
        CW_USEDEFAULT, CW_USEDEFAULT, 400, 300,
        NULL, NULL, hInstance, NULL
    );
    
    if (!hwnd) {
        return 0;
    }
    
    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);
    
    // 消息循环
    MSG msg = {};
    while (GetMessageW(&msg, NULL, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessageW(&msg);
    }
    
    return 0;
}